<?php
declare (strict_types=1);

namespace app\model\home;

use app\common\model\BaseModel;
use app\common\model\Member;
use app\common\model\Store;
use app\model\admin\Grade;
use \libs\Log;

class Assistant extends BaseModel
{
    protected $pk = 'id';

    protected function initialize()
    {
        parent::initialize();
    }

    protected function getGradeAttr($value)
    {
        return Grade::where('id', $value)->value('grade_name') ?: '';
    }

    /**
     * @param $value
     * @return array|false|string[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     */
    protected function getImageArrAttr($value)
    {
        $info = $this->where('id', $value)->field('avatar,album,cover')->find();
        if (empty($info['album'])) {
            !empty($info['cover']) && $album[] = $info['cover'];
            !empty($info['cover']) || $album[] = $info['avatar'];
        } else {
            $album = explode(',', $info['album']);
        }
        return $album;

    }

    /**
     * @param $value
     * @return mixed|string
     * 门店获取器
     */
    protected function getStoreNameAttr($value)
    {
        return Store::where('id', $value)->value('store_name') ?: '';
    }

    /**
     * @param $value
     * @return string
     * 格式化数字
     */
    public function getHotAttr($value)
    {
        return formatNumber($value);
    }

    /**
     * 获得列表
     * @param bool $where
     * @param string $fields
     * @param string|null $order
     * @param int $page
     * @param int $pageSize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($where = [], string $fields = '*', string $order = null, int $page = 1, int $pageSize = 20, $params = [])
    {
        $data = [
            'count' => 0,
            'list' => []
        ];
        if (empty($params)) {
            return $this->returnRight($data);
        }
        $count = $this->where($where)
            ->where(function ($query) use ($params) {
                if (!empty($params)) {
                    $query->where([$params]);
                }
            })->count();
        if (empty($count)) {
            return $this->returnRight($data);
        }
        $order = is_null($order) ? $this->pk . ' desc' : $order;;
        $list = $this->where($where)
            ->where(function ($query) use ($params) {
                if (!empty($params)) {
                    $query->where([$params]);
                }
            })
            ->field($fields)
            ->order($order)
            ->page($page, $pageSize)
            ->select();
        foreach ($list->toArray() as $item) {
            $item['virtual_avatar'] = get_rand_one_virtual_avatar();
            $listArr[] = $item;
        }
        $data['count'] = $count;
        $data['list'] = $listArr;
        return $this->returnRight($data);
    }

    /**
     * @param $data
     * @param string $fields
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 详情
     */
    public function getOne($data, string $fields = '*')
    {
        $info = self::where($this->pk, $data['id'])->field($fields)->find() ?: '';
        empty($info['cover']) && $info['cover'] = $info['avatar'];
        return $this->returnRight($info);
    }

    /**
     * @param array $where
     * @param string $fields
     * @param string|null $order
     * @param int $page
     * @param int $pageSize
     * @param array $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取随机列表
     */
    public function getRandomList($where = [], string $fields = '*', string $order = null, int $page = 1, int $pageSize = 20, $params = [])
    {
        $data = [
            'count' => 0,
            'list' => []
        ];
        $count = $this->where($where)->count();
        if (empty($count)) {
            return $this->returnRight($data);
        }
        $order = is_null($order) ? $this->pk . ' desc' : $order;;
        $list = $this->where($where)
            ->field($fields)
            ->order($order)
            ->page($page, $pageSize)
            ->select();
        $data['count'] = $count;
        $data['list'] = $list;
        return $this->returnRight($data);
    }

    /**
     * @param $data
     * 新增
     */
    public function add($data)
    {
        $map['store_id'] = $data['store_id'];
        $map['member_id'] = $data['member_id'];
        $count = $this->where($map)->count();
        if (!empty($count)) {
            return $this->returnError(32);
        }
        $this->startTrans();
        $res = self::create($data);
        $result = Member::update(['role' => 1], ['member_id' => $data['member_id']]);
        $this->commit();
        if (!empty($res) && !empty($result)) {
            return $this->returnSucc();
        } else {
            $this->rollback();
            return $this->returnError(1);
        }
    }
}
